package com.lingshu.app.ui.welcome;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProvider;

import com.lingshu.app.R;
import com.lingshu.app.base.BaseActivity;
import com.lingshu.app.databinding.ActivityWelcomeBinding;
import com.lingshu.app.ui.login.LoginActivity;

/**
 * @author 于定金
 * @version 1.0
 * @Date 2024/1/3 11:19
 * @Description
 */
public class WelcomeActivity extends BaseActivity<ActivityWelcomeBinding, WelcomeViewModel> {
    //通知运行时权限
    private static final int POST_NOTIFICATION_PERMISSION_REQUEST_CODE = 1;
    private static final int FOREGROUND_SERVICE_PERMISSION_REQUEST_CODE = 2;

    /**
     * 获取当前页面的布局资源ID
     *
     * @return 布局资源ID
     */
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_welcome;
    }

    /**
     * 初始化ViewModel
     */
    @Override
    protected void initViewModel() {
        viewModel = new ViewModelProvider(this).get(WelcomeViewModel.class);
    }

    /**
     * 绑定ViewModel
     */
    @Override
    protected void bindViewModel() {
        dataBinding.setViewModel(viewModel);
    }

    @Override
    protected void init() {
        super.init();

        // 检查通知运行时权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
            // 权限未被授予，请求权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, POST_NOTIFICATION_PERMISSION_REQUEST_CODE);
        } else {
            // 权限已被授予，执行相应操作
            onNotificationPermissionGranted();
        }

        // 检查通知运行时权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.FOREGROUND_SERVICE) != PackageManager.PERMISSION_GRANTED) {
            // 权限未被授予，请求权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.FOREGROUND_SERVICE}, FOREGROUND_SERVICE_PERMISSION_REQUEST_CODE);
        } else {
            // 权限已被授予，执行相应操作
            onForegroundServicePermissionGranted();
        }

        new Handler().postDelayed(() -> {
            startActivity(new Intent(getApplicationContext(), LoginActivity.class));
            finish();
            /**
             if (SharedUtils.getBoolean("isFirst", false)) {
             startActivity(new Intent(getApplicationContext(), LoginActivity.class));
             } else {
             startActivity(new Intent(getApplicationContext(), WelcomeGuideActivity.class));
             SharedUtils.putBoolean("isFirst", true);
             }*/
        }, 3000);
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == POST_NOTIFICATION_PERMISSION_REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 权限被授予，执行相应操作
                onNotificationPermissionGranted();
            } else {
                // 权限被拒绝，显示提示信息
                Toast.makeText(this, "未获得通知运行时权限", Toast.LENGTH_LONG).show();
            }
        }

        if (requestCode == FOREGROUND_SERVICE_PERMISSION_REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 权限被授予，执行相应操作
                onForegroundServicePermissionGranted();
            } else {
                // 权限被拒绝，显示提示信息
                Toast.makeText(this, "未获得前台服务权限", Toast.LENGTH_LONG).show();
            }
        }
    }

    private void onNotificationPermissionGranted() {
        //Toast.makeText(this, "已获得通知运行时权限", Toast.LENGTH_SHORT).show();
    }

    private void onForegroundServicePermissionGranted() {
        // 执行需要相机权限的操作，例如启动相机功能
        //Toast.makeText(this, "已获得前台服务权限", Toast.LENGTH_SHORT).show();
    }
}
